! Module necessary to Grell Cumulus param. 
! Scratch variables - module 2 

MODULE mem_scratch2_grell 

  !TYPE scratch2_grell_vars 

     !                   adapted in july-15-2002 for 5.x version 
     ! 
     ! 3d dependence (mgmxp,mgmyp,ensdim) 
     REAL, ALLOCATABLE, DIMENSION(:,:,:) :: massfln 

     ! 2d dependence (mgmxp,mgmzp) 
     REAL, ALLOCATABLE, DIMENSION(:,:) :: T,      & 
          Q,                                  & 
          P,                                  & 
          PO,                                 & 
          TN,                                 & 
          QO,                                 & 
          OUTT,                               & 
          OUTQ,                               & 
          outqc,                              & 
          US_Grell,                           & ! Substitui US original 
          VS_Grell,                           & ! Substitui VS original 
          omeg 
     ! 2d dependence (mgmxp, mgmyp) 
     INTEGER, ALLOCATABLE, DIMENSION(:,:) :: KDT, & 
          iact_gr,                            & 
          iact_old_gr 
     REAL, ALLOCATABLE, DIMENSION(:,:) :: xland,  & 
          massflx,                                &
          tkeg,                                   &
          rcpg 

     ! 1d dependence (mgmxp) 
     REAL, ALLOCATABLE, DIMENSION(:) :: mconv,    & 
          umean,                              & 
          vmean,                              & 
          pmean,                              & 
          direction 
     REAL, ALLOCATABLE, DIMENSION(:) ::       &
           AA0,                               & 
          PRET,                               & 
          PSUR,                               & 
          TER11,                              &
!srf-20082006 for training
          glatg,                              &
	  glong 
 
     INTEGER, ALLOCATABLE, DIMENSION(:) :: KDET 

  !END TYPE scratch2_grell_vars 

CONTAINS 

  SUBROUTINE alloc_scratch2_grell !(scratch2_grell) 

    USE mem_grell_param, ONLY : mgmxp,  & ! INTENT(IN) 
         mgmyp,                         & ! INTENT(IN) 
         mgmzp,                         & ! INTENT(IN) 
         ensdim                           ! INTENT(IN) 
    USE node_mod, ONLY : mynum, &   ! INTENT(IN) 
         MXP,                   &   ! INTENT(IN) 
         MYP,                   &   ! INTENT(IN) 
         MZP,                   &   ! INTENT(IN) 
         IA,                    &   ! INTENT(IN) 
         IZ,                    &   ! INTENT(IN) 
         JA,                    &   ! INTENT(IN) 
         JZ,                    &   ! INTENT(IN) 
         I0,                    &   ! INTENT(IN) 
         J0                         ! INTENT(IN) 

    IMPLICIT NONE 
    !TYPE (scratch2_grell_vars) :: scratch2_grell 

    INTEGER :: i 

    !WRITE(6,'(a1,78a1)') ' ',('_',i=1,78) 
    !PRINT*,' ' 
    !PRINT*,'---- In cuparth - alocando memoria--' 
    !PRINT*,'------------SCRATCH2----------------' 
    !PRINT*,'---------Node configuration---------' 
    !PRINT*,'MYNUM_I0____J0____=',mynum,i0,j0 
    !PRINT*,'mgmzp_mgmxp_mgmyp_=',mgmzp,mgmxp,mgmyp 
    !PRINT*,'m1____m2___m3_____=', mzp, mxp, myp !,m1,m2,m3 
    !PRINT*,'ia__iz____ja__jz__=',ia, iz, ja, jz 
    !PRINT*,'ensdim_____ialloc_=',ensdim !,ialloc 
    !PRINT*,' ' 
    !WRITE(6,'(a1,78a1)') ' ',('_',i=1,78) 

    ALLOCATE (massfln(mgmxp, mgmyp, ensdim)) 

    ALLOCATE (mconv    (mgmxp)) 
    ALLOCATE (umean    (mgmxp)) 
    ALLOCATE (vmean    (mgmxp)) 
    ALLOCATE (pmean    (mgmxp)) 
    ALLOCATE (direction(mgmxp)) 
    ALLOCATE (PRET     (mgmxp)) 
    ALLOCATE (PSUR     (mgmxp)) 
    ALLOCATE (TER11    (mgmxp)) 
!srf 20082006
    ALLOCATE (glatg    (mgmxp)) 
    ALLOCATE (glong    (mgmxp)) 

    ALLOCATE (AA0      (mgmxp)) 
    ALLOCATE (KDET     (mgmxp)) 

    ALLOCATE (T          (mgmxp, mgmzp)) 
    ALLOCATE (Q          (mgmxp, mgmzp)) 
    ALLOCATE (P          (mgmxp, mgmzp)) 
    ALLOCATE (PO         (mgmxp, mgmzp)) 
    ALLOCATE (TN         (mgmxp, mgmzp)) 
    ALLOCATE (QO         (mgmxp, mgmzp)) 
    ALLOCATE (OUTT       (mgmxp, mgmzp)) 
    ALLOCATE (OUTQ       (mgmxp, mgmzp)) 
    ALLOCATE (outqc      (mgmxp, mgmzp)) 
    ALLOCATE (US_Grell   (mgmxp, mgmzp)) 
    ALLOCATE (VS_Grell   (mgmxp, mgmzp)) 
    ALLOCATE (omeg       (mgmxp, mgmzp)) 
    ALLOCATE (KDT        (mgmxp, mgmyp)) 
    allocate (tkeg       (mgmxp, mgmzp))
    allocate (rcpg       (mgmxp, mgmzp))
    ALLOCATE (xland      (mgmxp, mgmyp)) 
    ALLOCATE (massflx    (mgmxp, mgmyp)) 
    ALLOCATE (iact_gr    (mgmxp, mgmyp)) 
    ALLOCATE (iact_old_gr(mgmxp, mgmyp)) 

    RETURN 
  END SUBROUTINE alloc_scratch2_grell 

  SUBROUTINE dealloc_scratch2_grell !(scratch2_grell) 

    IMPLICIT NONE 
    !TYPE (scratch2_grell_vars) :: scratch2_grell 

    DEALLOCATE (massfln) 

    DEALLOCATE (mconv) 
    DEALLOCATE (umean) 
    DEALLOCATE (vmean) 
    DEALLOCATE (pmean) 
    DEALLOCATE (direction) 

    DEALLOCATE (T) 
    DEALLOCATE (Q) 
    DEALLOCATE (P) 
    DEALLOCATE (PO) 
    DEALLOCATE (TN) 
    DEALLOCATE (QO) 
    DEALLOCATE (OUTT) 
    DEALLOCATE (OUTQ) 
    DEALLOCATE (outqc) 
    DEALLOCATE (PRET) 
    DEALLOCATE (PSUR) 
    DEALLOCATE (TER11) 
    DEALLOCATE (glatg) 
    DEALLOCATE (glong) 
    
    DEALLOCATE (US_Grell) 
    DEALLOCATE (VS_Grell) 
    DEALLOCATE (omeg) 
    DEALLOCATE (AA0) 

    DEALLOCATE (KDET) 
    DEALLOCATE (KDT) 

    DEALLOCATE (xland) 
    DEALLOCATE (massflx) 
    DEALLOCATE (iact_gr) 
    DEALLOCATE (iact_old_gr) 

    RETURN 
  END SUBROUTINE dealloc_scratch2_grell 
  
  SUBROUTINE zero_scratch2_grell() 
       massfln=0. 
       T=0. 
       Q=0. 
       P=0. 
       PO=0. 
       TN=0. 
       QO=0. 
       OUTT=0. 
       OUTQ=0. 
       outqc=0. 
       US_Grell=0. 
       VS_Grell=0. 
       omeg=0. 
       xland=0. 
       massflx=0. 
       tkeg=0.
       rcpg=0.
       mconv=0. 
       umean=0. 
       vmean=0. 
       pmean=0. 
       direction=0. 
       AA0=0. 
       PRET=0. 
       PSUR=0. 
       TER11=0. 
       glatg=0.
       glong=0.
       KDT=0 
       iact_gr=0 
       iact_old_gr=0 
       KDET=0 

  END SUBROUTINE zero_scratch2_grell 

!!$  SUBROUTINE filltab_scratch2_grell() 
!!$ 
!!$    USE var_tables 
!!$ 
!!$    IMPLICIT NONE 
!!$ 
!!$    ! Can't think of anything to do here... 
!!$ 
!!$    RETURN 
!!$  END SUBROUTINE filltab_scratch2_grell 

END MODULE mem_scratch2_grell 
